package com.bawei.mvvm;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import dagger.hilt.android.AndroidEntryPoint;

import android.os.Bundle;
import android.widget.Toast;

import com.bawei.mvvm.adapter.GoodsAdapter;
import com.bawei.mvvm.databinding.ActivityMainBinding;
import com.bawei.mvvm.entity.ApiResponse;
import com.bawei.mvvm.entity.Goods;
import com.bawei.mvvm.viewmodel.MainViewModel;

import java.util.ArrayList;
import java.util.List;
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding mActivityMainBinding;
    private MainViewModel mMainViewModel;
    private GoodsAdapter mGoodsAdapter;
    private List<Goods> mGoodsList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivityMainBinding =  DataBindingUtil.setContentView(this,R.layout.activity_main);
        mGoodsAdapter = new GoodsAdapter(mGoodsList);
        mActivityMainBinding.rv.setAdapter(mGoodsAdapter);
        mActivityMainBinding.rv.setLayoutManager(new LinearLayoutManager(this));


        mMainViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        mMainViewModel.goods(0,1,20).observe(this, new Observer<ApiResponse<List<Goods>>>() {
            @Override
            public void onChanged(ApiResponse<List<Goods>> listApiResponse) {
                int code = listApiResponse.getCode();
                if(code == 200){
                    Toast.makeText(MainActivity.this, "数据请求成功", Toast.LENGTH_SHORT).show();
                    mGoodsList.addAll(listApiResponse.getData());
                    mGoodsAdapter.notifyDataSetChanged();
                }else{
                    Toast.makeText(MainActivity.this, "数据请求失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}